package rental;

import users.*;
import lockers.*;
import java.util.Vector;
import OOJ.Console;


/**
 * 
 * @author erwin
 * 
 * Dit werkt waarschijnlijk beter als het een JUnit gebeuren gaat worden, test een stuk beter;
 *
 */
public class Program {
	
	private Vector<User> users;
	private Vector<Group> groups;
	private Vector<Locker> lockers;
	
	private User currentUser;
	
	private Program() {
		this.users = new Vector<User>();
		this.groups = new Vector<Group>();
		this.lockers = new Vector<Locker>();
	}
	/**
	 * @param args - empty
	 * 
	 */
	public static void main(String[] args) {
		System.out.println("Lockers rental program test");
		System.out.println("---------------------------");
		
		Program program = new Program();
		program.setup();
	}
	
	/**
	 * Setup waar bij 2 users elkaars kluisje proberen te openen. Geen authenticatietest alleen 
	 * authorisatie.
	 *
	 */
	private void setup() {
		/**
		User erwin = new User("Erwin");
		User martijn = new User("Martijn");
		OwnedLocker locker_erwin = new OwnedLocker("Sieraden", erwin);
		OwnedLocker locker_martijn = new OwnedLocker("2 Katten", martijn);
		this.users.add(erwin);
		this.users.add(martijn);
		this.lockers.add(locker_erwin);
		this.lockers.add(locker_martijn);
		
		**/
		this.run();
	}
	
	public void run() {
		String command;
		while(true) {
			
				command = Console.readString("LockerSys :> ");
				
				String[] commandTokens = command.split(" ");
				
			try {	
				// antwoorden
				//System.out.print("\n");
				
				if (command.equals("help")) {
					System.out.println("Help");
					System.out.println("---------------------------");
					System.out.println("addUser <name>");
					System.out.println("addPublicLocker <identifier>");
					System.out.println("addOwnedLocker <identifier> <username>");
					System.out.println("addGroup <name>");
					System.out.println("---------------------------");
					System.out.println("addToGroup <user> <group>");
					System.out.println("setLockerOwner <user> <lockeridentifier>");		
					System.out.println("setItem <lockeridentifier> <contents>");
					System.out.println("---------------------------");
					System.out.println("getGroups");
					System.out.println("getUsers");
					System.out.println("getLockers");
					System.out.println("getGroupInfo <group>");
					System.out.println("getUserInfo <user>");
					System.out.println("getLockerInfo <identifier>");
				} else if (commandTokens[0].equals("addUser")) {
					User newUser = new User(commandTokens[1]);
					this.users.add(newUser);
					System.out.println("User Added: " + newUser.toString());
				
				} else if (commandTokens[0].equals("addPublicLocker")) {
					PublicLocker tmpLocker = new PublicLocker(commandTokens[1]);
					this.lockers.add(tmpLocker);	
					System.out.println("PublicLocker Added: " + commandTokens[1]);
					
				} else if (commandTokens[0].equals("addOwnedLocker")) {
					for (int i=0;i<users.size();i++){
						if (users.get(i).toString().equals(commandTokens[2])) {
							OwnedLocker tmpLocker = new OwnedLocker(commandTokens[1], users.get(i));
							this.lockers.add(tmpLocker);
							
							System.out.println("OwnedLocker Added: " + users.get(i).toString() + " - " + commandTokens[1]);
							break;
						}
					}
				
				} else if (commandTokens[0].equals("addGroup")) {
					Group group = new Group(commandTokens[1]);
					this.groups.add(group);	
					System.out.println("Group Added: " + group.getName());			
					
					
				} else if (commandTokens[0].equals("addToGroup")) {
					for (int i=0;i<users.size();i++){
						if (users.get(i).toString().equals(commandTokens[1])) {
							for (int k=0;k<groups.size();k++){
								if (groups.get(k).getName().equals(commandTokens[2])) {									
									//System.out.println(commandTokens[0]);
									//System.out.println(commandTokens[1]);
									//System.out.println(commandTokens[2]);
									
									groups.get(k).addUser(users.get(i));
									System.out.println("User: " + users.get(i).toString() + " added to group " + groups.get(k).getName());
									break;
								}
							}
							
						}
					}
					
				} else if (commandTokens[0].equals("setLockerOwner")) {
					for (int i=0;i<users.size();i++){
						if (users.get(i).toString().equals(commandTokens[1])) {
							for (int j=0;j<lockers.size();j++){
								if (lockers.get(j).toString().equals(commandTokens[2])) {
									if (lockers.get(j) instanceof OwnedLocker) {
										((OwnedLocker)lockers.get(j)).setOwner(users.get(i));
										System.out.println("User: " + users.get(i).toString() + " added to locker " + lockers.get(j).toString());
										break;
									}
								}
							}
							
						}
					}			
					
				} else if (commandTokens[0].equals("setItem")) {
					for (int i=0;i<lockers.size();i++){
						if (lockers.get(i).toString().equals(commandTokens[1])) {
							lockers.get(i).setItem(commandTokens[2]);
							System.out.println("setItem:" + commandTokens[2] + " in locker " + lockers.get(i).toString());
							break;
						}	
					}
					
				} else if (commandTokens[0].equals("getGroups")) {
					for (int i=0;i<groups.size();i++){
						System.out.println("Group " + i + ": " + groups.get(i).getName());
					}
				} else if (commandTokens[0].equals("getUsers")) {
					for (int i=0;i<users.size();i++){
						System.out.println("User " + i + ": " + users.get(i).toString());
					}				
				} else if (commandTokens[0].equals("getLockers")) {
					for (int i=0;i<lockers.size();i++){
						System.out.println("Locker " + i + ": " + lockers.get(i).toString());
					}		
					
				} else if (commandTokens[0].equals("getGroupInfo")) {
					for (int i=0;i<groups.size();i++){
						if (groups.get(i).toString().equals(commandTokens[1])) {
							System.out.println("Group: " + groups.get(i).getName());
							
							Vector users = groups.get(i).getUsers();
							for (int j=0;j<users.size();j++){
								System.out.println("User " + j + ": " + users.get(j).toString());
							}
							break;
						}
					}				
					
				} else if (commandTokens[0].equals("getUserInfo")) {
					for (int i=0;i<users.size();i++){
						if (users.get(i).toString().equals(commandTokens[1])) {
							System.out.println("User:" + users.get(i).toString());
							for (int j=0;j<lockers.size();j++){
								if (lockers.get(j) instanceof OwnedLocker) {
									if (((OwnedLocker)lockers.get(j)).getOwner().toString().equals(users.get(i).toString())) {
										System.out.println("OwnedLocker:" + lockers.get(j).toString() + " - " + lockers.get(j).getItem());
									}
								}
							}
						}
					}
				} else if (commandTokens[0].equals("getLockerInfo")) {
					for (int j=0;j<lockers.size();j++){
						if (lockers.get(j).toString().equals(commandTokens[1])) {
							if (lockers.get(j) instanceof OwnedLocker) {
								System.out.println("OwnedLocker:" + lockers.get(j).toString() + " - " + ((OwnedLocker)lockers.get(j)).getOwner() + " - " + lockers.get(j).getItem());
							} else {
								System.out.println("OwnedLocker:" + lockers.get(j).toString() + " - " + lockers.get(j).getItem());
							}
						}
					}
				} else {
					System.out.println("Command not found");
				}
			} catch (Exception e) {
				System.out.println("Incorrect arguments - " + e.toString());
				//e.printStackTrace();
			}
		}
	}
	
	public User setCurrentUser(User user) {
		this.currentUser = user;
		return this.currentUser;
	}

}
